package com.guangzhi.weijianzhi.mainhome;

import org.apache.http.Header;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RatingBar;
import android.widget.TextView;
import android.widget.Toast;

import com.guangzhi.weijianzhi.R;
import com.guangzhi.weijianzhi.activity.BaseActivity;
import com.guangzhi.weijianzhi.context.WeiJianZhiEntity;
import com.guangzhi.weijianzhi.http.HttpRequestUtils;
import com.guangzhi.weijianzhi.http.MyAsyncHttpResponseHandler;
import com.guangzhi.weijianzhi.utils.Misc;

public class PositionDetailsActivity extends BaseActivity implements OnClickListener {

	private String mjobId;
	private TextView mTilte,mYetenroll,mDateDue,mAddress,mYtian,mTime,mDate,mAddreAdd,mIntro,mComName,mAlert_one,mAlert_two;
	private LinearLayout mLinInfo,mLinApply;
	private ImageView mImage;
	private RatingBar mCredit;
	private Button mApp_bu;
	private String mCompany_id;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		mjobId = getIntent().getStringExtra("jobid");
		setContentView(R.layout.tz_posi_details_layout);
		intiView();
		if("U".equals(WeiJianZhiEntity.userType)){
			mLinInfo.setVisibility(View.VISIBLE);
			mLinApply.setVisibility(View.VISIBLE);
		}else if("C".equals(WeiJianZhiEntity.userType)){
			mApp_bu.setVisibility(View.VISIBLE);
			mLinInfo.setVisibility(View.GONE);
			mLinApply.setVisibility(View.GONE);
		}else{
			
		}
		initData(mjobId);
	}

	private void intiView() {
		// TODO Auto-generated method stub
		initTitle();
		titleTextV.setText("职位详情");
		mLinInfo = (LinearLayout) findViewById(R.id.Lin_info);
		mLinApply = (LinearLayout) findViewById(R.id.Lin_apply);
		mTilte = (TextView) findViewById(R.id.title);
		mAddress = (TextView) findViewById(R.id.address);//地址
		 mYetenroll = (TextView) findViewById(R.id.yetenroll);//已报名
		 mDateDue = (TextView) findViewById(R.id.dateDue);//截止日期
		 mYtian = (TextView) findViewById(R.id.yuan_tian);//元天
		 mTime = (TextView) findViewById(R.id.time);//时间
		 mDate = (TextView) findViewById(R.id.date);//日期
		 mAddreAdd = (TextView) findViewById(R.id.addressAdd);//详细地址
		 mIntro = (TextView) findViewById(R.id.intro);//公司简介
		 mComName = (TextView) findViewById(R.id.com_name);//公司名字
		 mCredit = (RatingBar) findViewById(R.id.credit);//信用
		 mAlert_one = (TextView) findViewById(R.id.alert_one);//信用
		 mAlert_two = (TextView) findViewById(R.id.alert_two);//信用
		 mApp_bu = (Button) findViewById(R.id.apply_button);
		mImage = (ImageView) findViewById(R.id.image);
		findViewById(R.id.apply_buttwo).setOnClickListener(this);
		findViewById(R.id.apply_button).setOnClickListener(this);
		findViewById(R.id.consult).setOnClickListener(this);
		findViewById(R.id.apply).setOnClickListener(this);
		findViewById(R.id.collect).setOnClickListener(this);
		findViewById(R.id.compamy_info).setOnClickListener(this);
		goBack.setOnClickListener(this);
		
	}

	private void initData(String mjobId) {
		// TODO Auto-generated method stub
		HttpRequestUtils.doHttpHotTextDetails(mjobId, new MyAsyncHttpResponseHandler(getSupportFragmentManager(),getResources()) {
			@Override
			public void onSuccess(int arg0, Header[] arg1, byte[] arg2) {
				// TODO Auto-generated method stub
				super.onSuccess(arg0, arg1, arg2);
				try {
					JSONObject jsonO = new JSONObject(new String(arg2));
					
					if(jsonO.optBoolean("status")){
						JSONArray optJSONArray = jsonO.optJSONArray("data");
						
						if(optJSONArray.length()!=0){
							JSONObject optJSONObject = optJSONArray.optJSONObject(0);
					
						
						mTilte.setText(optJSONObject.optString("title"));//title
						mAddress.setText(optJSONObject.optString("area"));//地址
						mYetenroll.setText("已录取:\n"+optJSONObject.optString("apply_num"));//已报名
						mDateDue.setText(optJSONObject.optString("work_start_time"));//报名截止日期
						mYtian.setText(optJSONObject.optString("bonus")+"/"+optJSONObject.optString("bonus_type"));
						mTime.setText(optJSONObject.optString("job_time"));//工作时间
						mDate.setText(optJSONObject.optString("job_date"));//工作日期
						mCompany_id = optJSONObject.optString("company_id");
						mAddreAdd.setText(optJSONObject.optString("full_address"));//详细地址
						mIntro.setText(optJSONObject.optString("depict"));//公司简介
						mComName.setText(optJSONObject.optString("company_name"));//公司名字
						mAlert_one.setText(optJSONObject.optString("remind1"));//提示
						mAlert_two.setText(optJSONObject.optString("remind2"));//公司名字
						
						mCredit.setRating(Float.parseFloat(optJSONObject.optString("credit_level")));
						String add_service = optJSONObject.optString("sort_mark");
						if("1".equals(add_service)){
							mImage.setImageResource(R.drawable.gz_income_push);
						}else if("2".equals(add_service)){
							mImage.setImageResource(R.drawable.gz_income_ding);
						}else if("3".equals(add_service)){//todo
							
						}
					}
					}else{
						
						Misc.alert(jsonO.optString("errMessage"));
						
					}
					
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			
			@Override
			public void onFailure(int arg0, Header[] arg1, byte[] arg2,
					Throwable arg3) {
				// TODO Auto-generated method stub
				super.onFailure(arg0, arg1, arg2, arg3);
			}
			
		});
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.consult://咨询
            Intent intent = new Intent(Intent.ACTION_DIAL,Uri.parse("tel:"+"15737619971"));  
            startActivity(intent);  
			break;
		case R.id.apply://报名
			
			break;
		case R.id.collect://收藏
			
			break;
		case R.id.compamy_info:
			if(Misc.notNull(mCompany_id)){
				Intent intent1 = new Intent(this,CompanyInfoActivity.class);
				intent1.putExtra("comId", mCompany_id);
				startActivity(intent1);
			}

			break;
		case R.id.apply_buttwo:
			try {
				String url = "mqqwpa://im/chat?chat_type=wpa&uin=2088185030&version=1"; // uin为目标QQ号！
				startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
			} catch (Exception e) {
				Toast.makeText(this, "未找到QQ程序，请提前安装~",
						Toast.LENGTH_SHORT).show();
			}
			break;
		case R.id.apply_button:
			try {
				String url = "mqqwpa://im/chat?chat_type=wpa&uin=2088185030&version=1"; // uin为目标QQ号！
				startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
			} catch (Exception e) {
				Toast.makeText(this, "未找到QQ程序，请提前安装~",
						Toast.LENGTH_SHORT).show();
			}
			break;

		case R.id.title_back:
			onBackPressed();
			break;
		}
	}
	
}
